package com.shimao.celnet.payment.controller;

import com.shimao.celnet.payment.model.PPayCUPUser;
import com.shimao.celnet.payment.model.request.CallIn_OutPayCUPReqin;
import com.shimao.celnet.payment.service.BEstateService;

import com.shimao.celnet.payment.service.PPayCupUserService;
import com.shimao.celnet.payment.util.FinalUtil;
import com.shimao.celnet.payment.util.MyDateUtil;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**解绑访问接口
 * Created by XSW on 2018/11/2.
 */
@Controller
@RequestMapping(value = "payment")
public class CallIn_outUnbindBankController {

    protected Logger logger = Logger.getLogger(this.getClass());

    @Autowired
    private PPayCupUserService pPayCupUserService;

    @Autowired//小区
    private BEstateService bEstateService;

    @RequestMapping(value ="payUnbindBankCard",method = RequestMethod.GET)//跳转确认申请支付页面
    public String unbindBankCardPage(Model model, HttpServletRequest req, HttpServletResponse res){
        String userid = req.getParameter("userid");
        String payCustid = req.getParameter("payCustid");
        if(payCustid != null && userid!=null&& userid !="" && payCustid != "" ){
            payCustid = bEstateService.getBySfGuid(payCustid).getGuid();
            List<PPayCUPUser> pPayCUPUsers = pPayCupUserService.findCUPUserByMeruserid(userid,payCustid);
            if(pPayCUPUsers != null && pPayCUPUsers.size() > 0){
                for (PPayCUPUser p:
                pPayCUPUsers) {
                   p.setPayAcctno(MyDateUtil.hideCardNo(p.getPayAcctno()));
                }
                model.addAttribute("banks",pPayCUPUsers);
                model.addAttribute("cupguid",pPayCUPUsers.get(0).getGuid());
            }else {//没有卡时提醒他
                String guid = pPayCupUserService.getGuid();
                PPayCUPUser payCUPUser = new PPayCUPUser();
                payCUPUser.setGuid(guid);
                payCUPUser.setPayMeruserid(userid);
                payCUPUser.setPayCustid(payCustid);
                payCUPUser.setPayNeedStatus(FinalUtil.PAY_NEED_STATUS_NO); //1不需要支付
                pPayCupUserService.save(payCUPUser);
                model.addAttribute("cupguid",guid);
            }
        }
        return "cupPay/bindCard";
    }
}
